# Replication archive for: Coppock, Alexander and Seth J. Hill and Lynn Vavreck. 2020. "The Small Effects of Political Advertising are Small Regardless of Context, Message, Sender, or Receiver: Evidence from 59 Real-time Randomized Experiments" Science Advances, forthcoming.

library(tidyverse)
library(estimatr)
library(rmeta)
source("helpers.R")

favorability_sates <- read_rds("favorability_sates.rds")
vote_choice_sates <- read_rds("vote_choice_sates.rds")

sates_df <-
  bind_rows(Favorability = favorability_sates,
            `Vote Choice` = vote_choice_sates,
            .id = "dv") %>%
  mutate(est_type = "Weekly estimates") 
  
summary_df <-
  sates_df %>%
  group_by(dv) %>%
  do(random_effects_estimator(.)) %>%
  mutate(date = as.Date("2018-01-15"), 
         est_type = "Meta-analysis")

blank_df <-
  expand.grid(date = as.Date(c("2018-01-01", "2018-01-30"))) %>%
  mutate(est_type = "Meta-analysis",
         dv = "Favorability")


gg_df <- 
  bind_rows(sates_df, summary_df, blank_df) %>%
  mutate(est_type = factor(est_type,  c("Weekly estimates", "Meta-analysis")),
         est_entry = make_se_entry(estimate, std.error, 3),
         entry_y = estimate*5)


breaks <-
  as.Date(
    c(
      "2016-04-01",
      "2016-05-01",
      "2016-06-01",
      "2016-07-01",
      "2016-08-01",
      "2016-09-01",
      "2016-10-01",
      "2016-11-01"
    )
  )

g <-
  ggplot(gg_df, aes(date, estimate, group = ad_id)) +
  geom_point(alpha = 0.8, stroke = 0, position = position_dodge(5)) +
  geom_linerange(aes(ymin = conf.low, ymax = conf.high), alpha = 0.5, position = position_dodge(5)) +
  geom_hline(yintercept = 0, linetype = "dashed", alpha = 0.5) +
  geom_vline(xintercept = as.Date("2016-06-10"), linetype = "dotted") +
  geom_text(
    data = (. %>% filter(
      est_type == "Meta-analysis",!is.na(estimate)
    )),
    size = 2,
    aes(y = entry_y, 
        label = est_entry),
  ) +
  facet_grid(
    rows = vars(dv),
    cols = vars(est_type),
    scales = "free",
    space = "free_x"
  ) +
  scale_x_date(breaks = breaks, date_labels = "%B") +
  theme_bw() +
  theme(
    strip.background = element_blank(),
    axis.title.x = element_blank(),
    panel.grid.minor = element_blank(),
    text = element_text(size = 8),
    legend.position = c(0.6, 0.06),
    legend.box.background = element_rect(fill = "gray"),
    legend.key.size = unit(1, "lines"),
    legend.direction = "horizontal",
    legend.title = element_blank()
  ) +
  ylab("Average treatment effect estimate and 95% confidence interval") 
g



